package com.qinggeng.project.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.qinggeng.common.exception.CustomException;
import com.qinggeng.common.utils.DateUtils;
import com.qinggeng.common.utils.StringUtils;
import com.qinggeng.common.utils.spring.SpringUtils;
import com.qinggeng.project.system.domain.OrdersGift;
import com.qinggeng.project.system.domain.OrdersGoods;
import com.qinggeng.project.system.domain.OrdersPoint;
import com.qinggeng.project.system.domain.SinglePointSale;
import com.qinggeng.project.system.domain.vo.SinglePointSaleVo;
import com.qinggeng.project.system.mapper.SinglePointSaleMapper;
import com.qinggeng.project.system.service.SinglePointSaleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author 吴鹏飞
 * @date 2020/11/17
 * 配送区域单配送点销量业务实现
 */
@Service
@Slf4j
public class SinglePointSaleServiceImpl implements SinglePointSaleService {
    @Resource
    private SinglePointSaleMapper singlePointSaleMapper;


    @Override
    public SinglePointSale getSinglePointSaleById(Long id) {
        return singlePointSaleMapper.getSinglePointSaleById(id);
    }

    @Override
    public SinglePointSale getSinglePointSaleByPointIdAndGoodsIdAndRecordDate(Long pointId, Long goodsId, String recordDate) {
        return singlePointSaleMapper.getSinglePointSaleByPointIdAndGoodsIdAndRecordDate(pointId,goodsId,recordDate);
    }

    @Override
    public SinglePointSale getSinglePointSaleByPointIdAndGiftIdAndRecordDate(Long pointId, Long giftId, String recordDate) {
        return singlePointSaleMapper.getSinglePointSaleByPointIdAndGiftIdAndRecordDate(pointId,giftId,recordDate);
    }

    @Override
    public boolean addSinglePointSaleNum(Long id, Integer saleNum) {
        SinglePointSale singlePointSale=new SinglePointSale();
        singlePointSale.setId(id);
        singlePointSale.setSaleNum(saleNum);
        singlePointSale.setUpdateTime(DateUtils.getNowDate());
        return singlePointSaleMapper.addSinglePointSaleNum(singlePointSale);
    }

    @Override
    public boolean subtractSinglePointSaleNum(Long id, Integer saleNum) {
        SinglePointSale singlePointSale=new SinglePointSale();
        singlePointSale.setId(id);
        singlePointSale.setSaleNum(saleNum);
        singlePointSale.setUpdateTime(DateUtils.getNowDate());
        return singlePointSaleMapper.subtractSinglePointSaleNum(singlePointSale)>0;
    }

    @Override
    public List<SinglePointSale> listSinglePointSale(SinglePointSale singlePointSale) {
        return singlePointSaleMapper.listSinglePointSale(singlePointSale);
    }

    @Override
    public boolean insertSinglePointSale(SinglePointSale singlePointSale) {
        Date nowDate=DateUtils.getNowDate();
        singlePointSale.setCreateTime(nowDate);
        singlePointSale.setGoodsTime(nowDate);
        singlePointSale.setPrint("0");
        return singlePointSaleMapper.insertSinglePointSale(singlePointSale)>0;
    }

    @Override
    public int updateSinglePointSale(SinglePointSale singlePointSale) {
        return singlePointSaleMapper.updateSinglePointSale(singlePointSale);
    }

    @Override
    public int deleteSinglePointSaleById(Long id) {
        return singlePointSaleMapper.deleteSinglePointSaleById(id);
    }

    @Override
    public int deleteSinglePointSaleByIds(Long[] ids) {
        return singlePointSaleMapper.deleteSinglePointSaleByIds(ids);
    }

    /**
     * 查询配送区域单配送点销量集合
     * @param singlePointSale
     * @return
     */
    @Override
    public List<SinglePointSale> selectSinglePointSaleList(SinglePointSale singlePointSale) {
        return singlePointSaleMapper.select(singlePointSale);
    }

    @Override
    public List<SinglePointSale> listSinglePointSaleByTime(SinglePointSale where){
        List<SinglePointSale> singlePointSaleList=singlePointSaleMapper.listSinglePointSaleByTime(where);
        for (SinglePointSale singPointSale:singlePointSaleList) {
            if(StringUtils.isEmpty(where.getBeginTime())||StringUtils.isEmpty(where.getEndTime())){
                singPointSale.setRecordDate("全部时间");
            }else {
                singPointSale.setRecordDate(where.getBeginTime()+"至"+where.getEndTime());
            }
        }
        return singlePointSaleList;
    }
    /**
     * 获取配送区域单配送点销量详细信息
     * @param id
     * @return
     */
    @Override
    public SinglePointSale selectSinglePointById(Long id) {
        return singlePointSaleMapper.selectByPrimaryKey(id);
    }
    /**
     * 批量添加数据
     * @param singlePointSaleList
     */
    @Override
    public void insertSinglePointSaleList(List<SinglePointSale> singlePointSaleList) {
        singlePointSaleMapper.insertList(singlePointSaleList);
    }
    /**
     * 查询昨天的单自提点销量
     * @return
     */
    @Override
    public List<SinglePointSaleVo> listSinglePointSaleByYesterday(Long pointId,String recordDate) {
        PageHelper.startPage(1,20);
        List<SinglePointSaleVo> singlePointSaleVos = singlePointSaleMapper.listSinglePointSaleByYesterday(pointId,recordDate);
        return singlePointSaleVos;
    }
    /**
     * 打印自提点销量
     * @param
     * @return
     */
    @Override
    public boolean printSinglePointSaleList(Long id) {

        boolean bl=singlePointSaleMapper.printSinglePointSaleList(id)>0;
        if (bl ==false){
            throw  new CustomException(id+"更改状态失败");
        }
        return  true;
    }

    @Override
    public boolean printSinglePointByIds(String ids) {
        if (StringUtils.isEmpty(ids)){
            throw  new CustomException("ids is null");
        }
        String[] idArr=ids.split(",");
        StringBuilder stringBuilder=new StringBuilder();
        for (int i = 0; i < idArr.length; i++) {
            if (i<(idArr.length-1)){
                stringBuilder.append(idArr[i])
                             .append(",");
            }else {
                stringBuilder.append(idArr[i]);
            }
        }
        boolean bl=singlePointSaleMapper.printSinglePointByIds(ids)>0;
        return bl;
    }


    public void updateSinglePointSale(OrdersPoint ordersPoint,OrdersGoods ordersGoods, String recordDate){
        SinglePointSale singlePointSale=this.getSinglePointSaleByPointIdAndGoodsIdAndRecordDate(ordersPoint.getPointId(),ordersGoods.getGoodsId(),recordDate);
        if(singlePointSale!=null){
            boolean result=this.addSinglePointSaleNum(singlePointSale.getId(),ordersGoods.getGoodsNum());
            if(!result){
                log.error("增加销量失败！");
                throw new CustomException("增加销量失败！");
            }
            return;
        }
        singlePointSale=new SinglePointSale();
        singlePointSale.setSortArea(ordersPoint.getSortArea());
        singlePointSale.setPointId(ordersPoint.getPointId());
        singlePointSale.setPointName(ordersPoint.getPointName());
        singlePointSale.setGoodsId(ordersGoods.getGoodsId());
        singlePointSale.setGoodsName(ordersGoods.getGoodsName());
        singlePointSale.setAttributes(ordersGoods.getAttributes());
        singlePointSale.setSaleNum(ordersGoods.getGoodsNum());
        singlePointSale.setRecordDate(recordDate);
        //商品
        singlePointSale.setType(10);
        boolean b2=this.insertSinglePointSale(singlePointSale);
        if(!b2){
            log.error("增加销量失败！");
            throw new CustomException("增加销量失败");
        }
    }

    @Override
    public void updateSinglePointSale(OrdersPoint ordersPoint, OrdersGift ordersGift, String recordDate) {
        SinglePointSale singlePointSale=this.getSinglePointSaleByPointIdAndGiftIdAndRecordDate(ordersPoint.getPointId(),ordersGift.getGiftId(),recordDate);
        if(singlePointSale!=null){
            boolean result=this.addSinglePointSaleNum(singlePointSale.getId(),ordersGift.getGiftNum().intValue());
            if(!result){
                log.error("增加销量失败！");
                throw new CustomException("增加销量失败！");
            }
            return;
        }
        singlePointSale=new SinglePointSale();
        singlePointSale.setSortArea(ordersPoint.getSortArea());
        singlePointSale.setPointId(ordersPoint.getPointId());
        singlePointSale.setPointName(ordersPoint.getPointName());
        singlePointSale.setGoodsId(ordersGift.getGiftId());
        singlePointSale.setGoodsName(ordersGift.getGoodsName());
        singlePointSale.setAttributes(ordersGift.getGoodsName());
        singlePointSale.setSaleNum(ordersGift.getGiftNum().intValue());
        singlePointSale.setRecordDate(recordDate);
        singlePointSale.setType(20);
        boolean b2=this.insertSinglePointSale(singlePointSale);
        if(!b2){
            log.error("增加销量失败！");
            throw new CustomException("增加销量失败");
        }
    }
}
